package com.cloudOffice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloudOffice.model.process.Process;
import com.cloudOffice.model.process.ProcessTemplate;
import com.cloudOffice.model.process.ProcessType;
import com.cloudOffice.result.Result;
import com.cloudOffice.service.OaProcessService;
import com.cloudOffice.service.OaProcessTemplateService;
import com.cloudOffice.service.OaProcessTypeService;
import com.cloudOffice.service.SysUserService;
import com.cloudOffice.storage.CommonStorage;
import com.cloudOffice.vo.process.ApprovalVo;
import com.cloudOffice.vo.process.ProcessFormVo;
import com.cloudOffice.vo.process.ProcessVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Tag(name = "审批流管理")
@RestController("ProcessController")
@RequestMapping(value = "/admin/process")
@CrossOrigin //跨域
public class ProcessController {
	
	@Resource
	private OaProcessTypeService processTypeService;
	@Resource
	private OaProcessTemplateService processTemplateService;
	@Resource
	private OaProcessService processService;
	@Resource
	private SysUserService sysUserService;
	
	@Operation(summary = "待处理")
	@GetMapping("/findPending/{page}/{limit}")
	public Result<IPage<ProcessVo>> findPending(
			@Parameter(name = "page", description = "当前页码", required = true)
			@PathVariable Long page,
			@Parameter(name = "limit", description = "每页记录数", required = true)
			@PathVariable Long limit) {
		log.info("待处理：page: {}, limit: {}", page, limit);
		Page<Process> pageParam = new Page<>(page, limit);
		return Result.success(processService.findfindPending(pageParam));
	}
	
	@Operation(summary = "启动流程")
	@PostMapping("/startUp")
	public Result<?> startUp(@RequestBody ProcessFormVo processFormVo) {
		log.info("启动流程：processFormVo: {}", processFormVo);
		processService.startUp(processFormVo);
		return Result.success();
	}
	
	//获取审批模板数据
	@Operation(summary = "获取审批模板数据")
	@GetMapping("getProcessTemplate/{processTemplateId}")
	public Result<ProcessTemplate> getProcessTemplate(@PathVariable Long processTemplateId) {
		log.info("获取审批模板数据：processTemplateId: {}", processTemplateId);
		return Result.success(processTemplateService.getById(processTemplateId));
	}
	
	//查询所有审批分类和每个分类所有审批模板
	@GetMapping("findProcessType")
	public Result<List<ProcessType>> findProcessType() {
		log.info("查询所有审批分类和每个分类所有审批模板");
		return Result.success(processTypeService.findProcessType());
	}
	
	//查看审批详情信息
	@Operation(summary = "查看审批详情信息")
	@GetMapping("show/{id}")
	public Result<Map<String, Object>> show(@PathVariable Long id) {
		log.info("查看审批详情信息：id: {}", id);
		return Result.success(processService.show(id));
	}
	
	//审批
	@Operation(summary = "审批详情信息")
	@PostMapping("approve")
	public Result<?> approve(@RequestBody ApprovalVo approvalVo) {
		log.info("审批详情信息：approvalVo: {}", approvalVo);
		processService.approve(approvalVo);
		return Result.success();
	}
	
	@Operation(summary = "已处理")
	@GetMapping("/findProcessed/{page}/{limit}")
	public Result<IPage<ProcessVo>> findProcessed(
			@Parameter(name = "page", description = "当前页码", required = true)
			@PathVariable Long page,
			@Parameter(name = "limit", description = "每页记录数", required = true)
			@PathVariable Long limit) {
		log.info("已处理：page: {}, limit: {}", page, limit);
		Page<Process> pageParam = new Page<>(page, limit);
		return Result.success(processService.findProcessed(pageParam));
	}
	
	@Operation(summary = "已发起")
	@GetMapping("/findStarted/{page}/{limit}")
	public Result<IPage<ProcessVo>> findStarted(
			@Parameter(name = "page", description = "当前页码", required = true)
			@PathVariable Long page,
			@Parameter(name = "limit", description = "每页记录数", required = true)
			@PathVariable Long limit) {
		log.info("已发起：page: {}, limit: {}", page, limit);
		Page<ProcessVo> pageParam = new Page<>(page, limit);
		return Result.success(processService.findStarted(pageParam));
	}
	
	@Operation(summary = "获取当前用户信息")
	@GetMapping("getCurrentUser")
	public Result<Map<String, Object>> getCurrentUser() {
		log.info("获取用户信息：username: {}", CommonStorage.getUsername());
		return Result.success(sysUserService.getUserInfo(CommonStorage.getUsername()));
	}
}
